<template>
	<view class="addUser">
		<uni-forms :modelValue="formData" border>
			<uni-forms-item label="姓名">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="部门">
				<uni-easyinput type="text" v-model="formData.department" placeholder="请输入部门" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="电话">
				<uni-easyinput type="text" v-model="formData.phone" placeholder="请输入电话" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="职务">
				<uni-data-picker :localdata="items" popup-title="请选择职务" v-model="formData.type">
				</uni-data-picker>
			</uni-forms-item>
		</uni-forms>
		<view class="buttonWrap">
			<button type="primary" size="mini" @click="handleSubmit">提 交</button>
			<button size="mini" @click="handleCancel">取 消</button>
		</view>
	</view>
</template>

<script>
	const warehouse = {
		"6": ["管理者", "业务", "财务", "配送", "专卖店", "仓储", "配送负责人"],
		"0": ["业务负责人", "业务主管", "业务"],
		"1": ["财务负责人", "财务-入库", "财务"],
		"3": ["物流负责人", "仓储", "配送"],
		"4": ["专卖店负责人", "专卖店"],
	}
	export default {
		name: "addUser",
		data() {
			return {
				formData: {
					name: "",
					type: "",
					phone: "",
					department: "",
				},
				items: []
			}
		},
		onLoad() {
			// 根据类型生成职位
			this.items = warehouse[this.userType].map(item => {
				const obj = {}
				obj.text = obj.value = item
				return obj
			})
		},
		methods: {
			async handleSubmit() {
				try {
					await this.request("userCreate", {
						name: this.formData.name,
						type: this.formData.type,
						phone: this.formData.phone,
						department: this.formData.department,
					})

					uni.showModal({
						content: "创建成功",
						showCancel: false,
						success(res) {
							if (res.confirm) {
								uni.navigateBack()
							}
						}
					})
				} catch (e) {
					console.log(e)
				}
			},
			handleCancel() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.addUser {
		padding: 30rpx;
	}
</style>
